package service;

import dao.CroRoleDao;
import entity.CroRole;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import vo.CroRoleVo;

import javax.annotation.Resource;
import java.util.List;

/**
 * Created by xuweijie on 2017/4/7.
 */
@Service
public class CroRoleService {


    @Autowired
    private CroRoleDao croRoleDao;


    /**
     * 查找出全部角色
     * */
    @Transactional(readOnly = true)
    public List<CroRole> FindAllRole(){
        String hql="from CroRole";
        List<CroRole> croRoles=croRoleDao.findList(hql);
        return croRoles;
    }

    /*
    * 自定义vo类来查找出全部角色
    * */
    @Transactional(readOnly = true)
    public  List<CroRoleVo> FindAllRoleVo(){
        String hql="from CroRole";
        List<CroRoleVo> croRoles=croRoleDao.findByHqlExt(hql,CroRoleVo.class);
        return  croRoles;
    }

    /*
    *根据id查找出相应的角色
    * */
    @Transactional(readOnly = true)
    public CroRole FindRoleById(String id){
        CroRole croRole=croRoleDao.getById(id);
        return croRole;
    }


    /**
     * 添加新角色
     * */
    @Transactional(readOnly = false)
    public String add(CroRole croRole){
        String str = (String) croRoleDao.save(croRole);
        return str;
    }

    /**
     * 更新角色信息
     * */
    @Transactional(readOnly = false)
    public boolean updateRole(CroRole croRole){
        try{
            croRoleDao.update(croRole);
            return true;
        }catch (Exception e){
            e.printStackTrace();
            return false;
        }
    }

    /**
     * 根据id删除角色
     * */
    @Transactional(readOnly = false)
    public boolean delRoleById(String id){
        try{
            croRoleDao.delete(id);
            return true;
        }catch (Exception e){
            e.printStackTrace();
            return false;
        }
    }

}
